package cn.edu.neusoft.demo.hospital2.Controller;

import cn.edu.neusoft.demo.hospital2.Service.DepartmentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import cn.edu.neusoft.demo.hospital2.pojo.dto.*;

import java.util.List;

@RestController
@CrossOrigin(origins = "*")
@Tag(name = "管理员科室管理相关的api")
@RequestMapping("/api/departments")
public class DepartmentManagementController {

    @Autowired
    private DepartmentService departmentService;

    public DepartmentManagementController(DepartmentService departmentService) {
        this.departmentService = departmentService;
    }

    // 获取科室树形结构
    @Operation(summary = "获取科室树形结构",description = "获取科室树形结构包括诊室以及特色诊疗")
    @GetMapping("/tree")
    public ResponseEntity<ApiResponse<List<DepartmentTreeDTO>>> getDepartmentTree() {
        try {
            List<DepartmentTreeDTO> tree = departmentService.getDepartmentTree();
            return ResponseEntity.ok(ApiResponse.success(tree));
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(ApiResponse.error("获取科室树失败: " + e.getMessage()));
        }
    }

    // 获取所有科室列表
    @Operation(summary = "获取所有科室列表",description = "获取所有科室列表")
    @GetMapping
    public ResponseEntity<ApiResponse<List<DepartmentDTO>>> getAllDepartments() {
        try {
            List<DepartmentDTO> departments = departmentService.getAllDepartments();
            return ResponseEntity.ok(ApiResponse.success(departments));
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(ApiResponse.error("获取科室列表失败: " + e.getMessage()));
        }
    }

    // 获取单个科室详情
    @Operation(summary = "获取单个科室详情",description = "获取单个科室详情")
    @GetMapping("/{id}")
    public ResponseEntity<ApiResponse<DepartmentDTO>> getDepartmentById(@PathVariable Integer id) {
        try {
            DepartmentDTO department = departmentService.getDepartmentById(id);
            return ResponseEntity.ok(ApiResponse.success(department));
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND)
                    .body(ApiResponse.error(e.getMessage()));
        }
    }

    // 创建新科室
    @Operation(summary = "创建新科室",description = "创建新科室")
    @PostMapping
    public ResponseEntity<ApiResponse<Boolean>> createDepartment(@RequestBody DepartmentCreateDTO dto) {
        try {
            boolean result = departmentService.createDepartment(dto);
            return ResponseEntity.ok(ApiResponse.success(result));
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST)
                    .body(ApiResponse.error(e.getMessage()));
        }
    }

    // 更新科室
    @Operation(summary = "更新科室",description = "更新科室")
    @PutMapping("/{id}")
    public ResponseEntity<ApiResponse<Boolean>> updateDepartment(
            @PathVariable Integer id,
            @RequestBody DepartmentUpdateDTO dto) {
        try {
            if (!id.equals(dto.getDepartmentId())) {
                throw new IllegalArgumentException("ID不匹配");
            }
            boolean result = departmentService.updateDepartment(dto);
            return ResponseEntity.ok(ApiResponse.success(result));
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST)
                    .body(ApiResponse.error(e.getMessage()));
        }
    }

    // 删除科室
    @Operation(summary = "删除科室",description = "删除科室")
    @DeleteMapping("/{id}")
    public ResponseEntity<ApiResponse<Boolean>> deleteDepartment(@PathVariable Integer id) {
        try {
            boolean result = departmentService.deleteDepartment(id);
            return ResponseEntity.ok(ApiResponse.success(result));
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST)
                    .body(ApiResponse.error(e.getMessage()));
        }
    }

    // 获取科室关联的诊室
    @Operation(summary = "获取科室关联的诊室",description = "获取科室关联的诊室")
    @GetMapping("/{id}/rooms")
    public ResponseEntity<ApiResponse<List<RoomDTO>>> getDepartmentRooms(@PathVariable Integer id) {
        try {
            List<RoomDTO> rooms = departmentService.getRoomsByDepartmentId(id);
            return ResponseEntity.ok(ApiResponse.success(rooms));
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(ApiResponse.error("获取诊室列表失败: " + e.getMessage()));
        }
    }

    // 获取科室关联的特色门诊
    @Operation(summary = "获取科室关联的特色门诊",description = "获取科室关联的特色门诊")
    @GetMapping("/{id}/clinics")
    public ResponseEntity<ApiResponse<List<ClinicDTO>>> getDepartmentClinics(@PathVariable Integer id) {
        try {
            List<ClinicDTO> clinics = departmentService.getClinicsByDepartmentId(id);
            return ResponseEntity.ok(ApiResponse.success(clinics));
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(ApiResponse.error("获取特色门诊失败: " + e.getMessage()));
        }
    }
}